<template>
  <div class="wrapper" >
    <div style="margin: 60px auto; background-color:  rgba(255,255,255,0.5); width: 500px; height: 760px; padding: 8px 20px; border-radius: 10px;">
      <div style="margin: 10px 0; text-align: center; font-size: 30px;color: #3F5EFB"><b>注 册</b></div>
      <el-upload
          style="text-align: center"
          class="avatar-uploader"
          action="http://localhost:9091/file/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
      >
        <img v-if="user.avatarUrl" :src="user.avatarUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>

      <el-form :model="user" :rules="rules" ref="userForm" style="display: block">
        <el-form-item prop="username" >
          <el-input placeholder="请输入账号" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="nickname" >
          <el-input placeholder="请输入昵称" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user" v-model="user.nickname"></el-input>
        </el-form-item>
        <el-form-item prop="phone" >
          <el-input placeholder="请输入手机号 (选填)" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user" v-model="user.phone"></el-input>
        </el-form-item>
        <el-form-item prop="email" >
          <el-input placeholder="请输入邮箱 (选填)" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user" v-model="user.email"></el-input>
        </el-form-item>
        <el-form-item prop="address" >
          <el-input placeholder="请输入地址 (选填)" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user" v-model="user.address"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" size="medium" style="margin: 5px 0" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input placeholder="请确认密码" size="medium" style="margin: 5px 0" prefix-icon="el-icon-lock" show-password v-model="user.confirmPassword"></el-input>
        </el-form-item>
        <el-form-item style="margin: 10px 0; text-align: center">
          <el-button type="primary" size="small"  autocomplete="off" @click="login">确定</el-button>
          <el-button type="warning" size="small"  autocomplete="off"  @click="$router.push('/login')">返回登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      bgcUrl:"",
      form:{},
      user: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    // this.request.get("https://bing.biturl.top/?resolution=1920&format=json&index=0&mkt=zh-CN").then(res=>{
    //   console.log(res)
    //   this.bgcUrl=res.url
    //   console.log(this.bgcUrl)
    // })
  },
  methods: {
    login() {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {  // 表单校验合法
          if (this.user.password !== this.user.confirmPassword) {
            this.$message.error("两次输入的密码不一致")
            return false
          }
          this.request.post("/user/register", this.user).then(res => {
            if(res.code === '200') {
              this.$message.success("注册成功")
              this.$router.push('/login')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      });
    },
    handleAvatarSuccess(res){
      this.user.avatarUrl = res
      this.$message.success("上传成功，请继续！")
      console.log(res)
    }
  }
}
</script>

<style>
.wrapper {
  height: 100vh;
  /*background-image: linear-gradient(to bottom right, #FC466B , #3F5EFB);*/
  background: url(../assets/img/bgcpicc.jpg) center no-repeat;
  overflow: hidden;
}

.avatar-uploader{
  margin-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 118px;
  height: 118px;
  line-height: 118px;
  text-align: center;
}
.avatar {
  width: 118px;
  height: 118px;
  display: block;
}
</style>
